package cn.com.aiton.educenter.controller;

import cn.com.aiton.educenter.config.OperLog;
import cn.com.aiton.educenter.entity.PestsProject;
import cn.com.aiton.educenter.entity.UcenterMember;
import cn.com.aiton.educenter.entity.vo.RegisterVo;
import cn.com.aiton.educenter.service.PestsProjectService;
import cn.com.aiton.educenter.service.UcenterMemberService;
import cn.com.qiter.common.JwtUtils;
import cn.com.qiter.common.Result;
import cn.com.qiter.common.log.OperConstant;
import cn.com.qiter.common.ordervo.UcenterMemberOrder;
import cn.com.aiton.educenter.entity.vo.UcenterMemberQuery;
import cn.com.qiter.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testAuthor
 * @since 2021-02-28
 */
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {
    @Autowired
    PestsProjectService pestsProjectService;
    @Autowired
    private UcenterMemberService memberService;
    //登录
    @ApiOperation(value = "登录")
    @OperLog(type = OperConstant.SELECT,module = "UcenterMemberController",requestDes = "登录")
    @GetMapping("wxlogin")
    @ResponseBody
    public Result wxlogin(String phone, String password, boolean isLogin) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        UcenterMember um = new UcenterMember();
        um.setMobile(phone);
        um.setPassword(password);
        String token = memberService.login(um);
        return Result.ok().data("token",token);
    }
    //登录
    @ApiOperation(value = "登录")
    @OperLog(type = OperConstant.SELECT,module = "UcenterMemberController",requestDes = "登录")
    @GetMapping("login/{username}/{password}")
    @ResponseBody
    public Result login(@PathVariable String username, @PathVariable String password) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        UcenterMember um = new UcenterMember();
        um.setMobile(username);
        um.setPassword(password);
        String token = memberService.login(um);
        return Result.ok().data("token",token);
    }

    //注册
    @ApiOperation(value = "注册")
    @OperLog(type = OperConstant.ADD,module = "UcenterMemberController",requestDes = "注册")
    @PostMapping("register")
    public Result registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return Result.ok();
    }

    //根据token获取用户信息
    @ApiOperation(value = "根据token获取用户信息")
    @OperLog(type = OperConstant.SELECT,module = "UcenterMemberController",requestDes = "根据token获取用户信息")
    @GetMapping("getMemberInfo")
    public Result getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        UcenterMemberOrder umm = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,umm);
        return Result.ok().data("userInfo",umm);
    }

    //根据用户id获取用户信息
    @ApiOperation(value = "根据用户id获取用户信息")
    @OperLog(type = OperConstant.SELECT,module = "UcenterMemberController",requestDes = "根据用户id获取用户信息")
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        //把member对象里面值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    @ApiOperation(value = "登出")
    @OperLog(type = OperConstant.FUNCTION,module = "UcenterMemberController",requestDes = "登出")
    @PostMapping("logout")
    public Result logout(){
        return Result.ok();
    }

    //查询某一天注册人数
    @ApiOperation(value = "查询某一天注册人数")
    @OperLog(type = OperConstant.SELECT,module = "UcenterMemberController",requestDes = "查询某一天注册人数")
    @GetMapping("countRegister/{day}")
    public Result countRegister(@PathVariable String day) {
        Integer count = memberService.countRegisterDay(day);
        return Result.ok().data("countRegister",count);
    }

    //1 查询用户表所有数据
    //rest风格
    @ApiOperation(value = "查询用户表所有数据")
    @OperLog(type = OperConstant.SELECT,module = "UcenterMemberController",requestDes = "查询用户表所有数据")
    @GetMapping("findAll")
    public Result findAll() {
        //调用service的方法实现查询所有的操作
        List<UcenterMember> list = memberService.list(null);
        return Result.ok().data("items",list);
    }

    //2 逻辑删除讲师的方法

    @ApiOperation(value = "删除")
    @OperLog(type = OperConstant.DELTE,module = "UcenterMemberController",requestDes = "删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@ApiParam(name = "id", value = "ID", required = true)
                         @PathVariable String id) {
        boolean flag = memberService.removeById(id);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    //3 分页查询讲师的方法
    //current 当前页
    //limit 每页记录数
    @ApiOperation(value = "分页查询")
    @OperLog(type = OperConstant.SELECT,module = "UcenterMemberController",requestDes = "分页查询")
    @GetMapping("page/{current}/{limit}")
    public Result page(@PathVariable long current,
                       @PathVariable long limit) {
        //创建page对象
        Page<UcenterMember> pageTeacher = new Page<>(current,limit);

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        memberService.page(pageTeacher,null);

        long total = pageTeacher.getTotal();//总记录数
        List<UcenterMember> records = pageTeacher.getRecords(); //数据list集合


        return Result.ok().data("total",total).data("rows",records);
    }

    //4 条件查询带分页的方法
    @ApiOperation(value = "分页查询")
    @OperLog(type = OperConstant.SELECT,module = "UcenterMemberController",requestDes = "分页查询")
    @PostMapping("page/condition/{current}/{limit}")
    public Result pageCondition(@PathVariable long current,@PathVariable long limit,
                                @RequestBody(required = false) UcenterMemberQuery teacherQuery) {
        //创建page对象
        Page<UcenterMember> pageTeacher = new Page<>(current,limit);

        //构建条件
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String mobile = teacherQuery.getMobile();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(mobile)) {
            //构建条件
            wrapper.like("mobile",mobile);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }

        //排序
        wrapper.orderByDesc("stime");

        //调用方法实现条件查询分页
        memberService.page(pageTeacher,wrapper);

        long total = pageTeacher.getTotal();//总记录数
        List<UcenterMember> records = pageTeacher.getRecords(); //数据list集合
        return Result.ok().data("total",total).data("rows",records);
    }

    //添加讲师接口的方法
    @ApiOperation(value = "新增加")
    @OperLog(type = OperConstant.ADD,module = "UcenterMemberController",requestDes = "新增")
    @PostMapping("save")
    public Result save(@RequestBody UcenterMember eduTeacher) {
        boolean save = memberService.save(eduTeacher);
        if(save) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    //根据讲师id进行查询
    @ApiOperation(value = "根据ID查询信息")
    @OperLog(type = OperConstant.SELECT,module = "UcenterMemberController",requestDes = "根据ID查询信息")
    @GetMapping("getOne/{id}")
    public Result getOne(@PathVariable String id) {
        UcenterMember eduTeacher = memberService.getById(id);
        return Result.ok().data("teacher",eduTeacher);
    }

    //讲师修改功能
    @ApiOperation(value = "用户修改功能")
    @OperLog(type = OperConstant.UPDATE,module = "UcenterMemberController",requestDes = "用户修改功能")
    @PostMapping("update")
    public Result update(@RequestBody UcenterMember eduTeacher) {
        boolean flag = memberService.updateById(eduTeacher);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "切换默认项目")
    @OperLog(type = OperConstant.FUNCTION,module = "UcenterMemberController",requestDes = "切换默认项目")
    @GetMapping("switchProject/{userId}/{projectId}")
    public Result switchProject(@PathVariable String userId,@PathVariable String projectId) {
        UcenterMember member = memberService.getById(userId);
        PestsProject pp = pestsProjectService.getById(projectId);
        if (null != pp && member != null){
            member.setProjectId(projectId);
            boolean flag = memberService.updateById(member);
            if(flag) {
                return Result.ok();
            } else {
                return Result.error();
            }
        }else{
            return Result.error();
        }

    }
    /**
     * //根据用户id进行查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据IP查询项目")
    @OperLog(type = OperConstant.SELECT,module = "UcenterMemberController",requestDes = "根据IP查询项目")
    @GetMapping("getProjects/{id}")
    public Result getProjects(@PathVariable String id) {
        List<PestsProject>  list = memberService.selectProjectByMember(id);
        return Result.ok().data("rows",list);
    }
    /**
     * //根据用户id进行查询
     * @param id
     * @return
     */
    @ApiOperation(value = "登录")
    @OperLog(type = OperConstant.SELECT,module = "UcenterMemberController",requestDes = "登录")
    @GetMapping("getMembers/{id}")
    public Result getMembers(@PathVariable String id) {
        List<UcenterMember>  list = memberService.selectMemberByProject(id);
        return Result.ok().data("rows",list);
    }
}

